package com.dhh.dao;

import java.util.ArrayList;
import java.util.List;
import com.dhh.util.StringUtil;
import com.dhh.vo.FoodDiet;
import com.dhh.vo.PlanItem;
import android.content.ContentValues;
import android.database.Cursor;

public class DietDAO {
	private DataBaseHelper db;
	
	public DietDAO(DataBaseHelper db){
		this.db = db;
	}
	
	public boolean removeDiet(String id){
		boolean isSuc = false;
		
		try{
			db.getWritableDatabase().delete("food_diet", "_id=?",new String[] {id});
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return isSuc;
	}
	
	public boolean removePlanItem(String id){
		boolean isSuc = false;
		
		try{
			db.getWritableDatabase().delete("daily_food_plan_item", "_id=?",new String[] {id});
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return isSuc;
	}
	
	public PlanItem getPlanItem(String id){
		return null;
	}
	
	public List<PlanItem> listAll(){
		List<PlanItem> lst = new ArrayList<PlanItem>();
		try{
			String[] columns = PlanItem.getColumns(); 
			Cursor cursor = db.getReadableDatabase().query("daily_food_plan_item", columns, null, null, null,null, null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					PlanItem item = new PlanItem(cursor.getString(0),cursor.getString(1),StringUtil.cutString(cursor.getString(2),0),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6));
					lst.add(item);
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return lst;
	}
	
	public List<PlanItem> listPlanItem(String periodType,String diet){
		List<PlanItem> lst = new ArrayList<PlanItem>();
		try{
			String whereSQL = "1=1";
			if(periodType!=null){
				whereSQL += " and "+PlanItem.C_PERIOD+"='"+periodType+"'";
			}
			if(diet!=null){
				whereSQL += " and "+PlanItem.C_PARENT+"='"+diet+"'";
			}
			String[] columns = PlanItem.getColumns(); 
			Cursor cursor = db.getReadableDatabase().query(true, "daily_food_plan_item", columns, whereSQL,null, null, null, null, null);
			
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					PlanItem item = new PlanItem(cursor.getString(0),cursor.getString(1),StringUtil.cutString(cursor.getString(2),0),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6));
					lst.add(item);
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return lst;
	}
	
	public FoodDiet getDiet(String id){
		FoodDiet diet = new FoodDiet();
		if(id==null || id.length()==0)
			return null;
		try{
			String whereSQL = "_id="+id;
			Cursor cursor = db.getReadableDatabase().query(true, "food_diet", FoodDiet.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				diet.setId(cursor.getString(0));
				diet.setDate(cursor.getString(1));
				diet.setName(cursor.getString(2));
				diet.setPlanId(cursor.getString(3));
				diet.setBreakFast(listPlanItem("Breakfast", diet.getId()));
				diet.setLunch(listPlanItem("Lunch", diet.getId()));
				diet.setDinner(listPlanItem("Dinner", diet.getId()));
				diet.setSnack(listPlanItem("Snack", diet.getId()));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return diet;
	}
	
	public FoodDiet getDietByRowId(long rowid){
		FoodDiet diet =  null;
		try{
			String whereSQL = "_ROWID_="+rowid;
			Cursor cursor = db.getReadableDatabase().query(true, "food_diet", FoodDiet.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				diet = new FoodDiet();
				diet.setId(cursor.getString(0));
				diet.setDate(cursor.getString(1));
				diet.setName(cursor.getString(2));
				diet.setPlanId(cursor.getString(3));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return diet;
	}
	
	public List<FoodDiet> getAllDiet(){
		List<FoodDiet> lst = new ArrayList<FoodDiet>();
		try{
			String[] columns = FoodDiet.getColumns(); 
			Cursor cursor = db.getReadableDatabase().query(true, "food_diet", columns, null,null, null, null, "date desc", null);
			
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					FoodDiet item = new FoodDiet(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));
					lst.add(item);
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return lst;
	}
	
	public boolean addPlanItem(PlanItem item) {
		boolean isSuc = false;
		ContentValues initialValues = createContentValues(item);
		try{
			db.openDataBase();
			db.getWritableDatabase().insert("daily_food_plan_item", null, initialValues);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return isSuc;
	}
	
	public boolean addDiet(FoodDiet diet){
		boolean flag = false;
		ContentValues initialValues = createContentValues(diet);
		try{
			db.getWritableDatabase().insert("FOOD_DIET", null, initialValues);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
		
	}
	
	public FoodDiet addDietAndRet(FoodDiet diet){
		FoodDiet rd = null;
		long rowid = 0;
		ContentValues initialValues = createContentValues(diet);
		try{
			rowid = db.getWritableDatabase().insert("FOOD_DIET", null, initialValues);
			rd = this.getDietByRowId(rowid);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return rd;
		
	}
	
	private ContentValues createContentValues(PlanItem item) {
		ContentValues values = new ContentValues();
		if(item.getPeriod()!=null && item.getPeriod().trim().length()>0)
			values.put(PlanItem.C_PERIOD, item.getPeriod());
		if(item.getFoodName()!=null && item.getFoodName().trim().length()>0)
			values.put(PlanItem.C_FOOD_NAME, item.getFoodName());
		if(item.getFoodGroup()!=null && item.getFoodGroup().trim().length()>0)
			values.put(PlanItem.C_FOOD_GROUP, item.getFoodGroup());
		if(item.getQuatity()!=null && item.getQuatity().trim().length()>0)
			values.put(PlanItem.C_QUATITY, item.getQuatity());
		if(item.getEnergy()!=null && item.getEnergy().trim().length()>0)
			values.put(PlanItem.C_ENERGY, item.getEnergy());
		if(item.getParent()!=null && item.getParent().trim().length()>0)
			values.put(PlanItem.C_PARENT, item.getParent());
		return values;
	}
	
	private ContentValues createContentValues(FoodDiet diet) {
		ContentValues values = new ContentValues();
		if(diet.getId()!=null)
			values.put(FoodDiet.C_ID, diet.getId());
		if(diet.getDate()!=null)
			values.put(FoodDiet.C_DATE, diet.getDate());
		if(diet.getName()!=null)
			values.put(FoodDiet.C_NAME,diet.getName());
		if(diet.getPlanId()!=null)
			values.put(FoodDiet.C_PLAN,diet.getPlanId());
		return values;
	}
}
